package cn.com.egova.web.action;

import cn.com.egova.domain.PageBean;
import cn.com.egova.domain.SaleVisit;
import cn.com.egova.service.SaleVisitService;
import com.opensymphony.xwork2.ModelDriven;
import freemarker.template.SimpleDate;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Created by yuanchao on 2020/5/27.
 */
public class SaleVisitAction extends BaseAction implements ModelDriven<SaleVisit> {
    private static final long serialVersionUID = 3289435043249234l;
    private SaleVisit saleVisit = new SaleVisit();
    private SaleVisitService saleVisitService;
    private String startTime;
    private String endTime;

    public void setSaleVisitService(SaleVisitService saleVisitService) {
        this.saleVisitService = saleVisitService;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    @Override
    public SaleVisit getModel() {
        return saleVisit;
    }


    public String findByPage() throws ParseException {
        DetachedCriteria criteria = DetachedCriteria.forClass(SaleVisit.class);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (startTime != null && !startTime.trim().isEmpty()) {

            criteria.add(Restrictions.ge("visit_time", format.parse(startTime)));
        }
        if (endTime != null && !endTime.trim().isEmpty()) {
            criteria.add(Restrictions.le("visit_time", format.parse(endTime)));
        }
        PageBean<SaleVisit> page = saleVisitService.findByPage(this.getPageCode(), this.getPageSize(), criteria);
        this.setValueStack("page", page);
        return "page";
    }

    public String initAddUI() {
        return "initAddUI";
    }

    public String save() {
        saleVisitService.save(saleVisit);
        return "save";
    }
}
